is_a
Check if an object belongs to this class, or to use this class as one of its parent classes: Return true if the object belongs to that class or is the parent class of this object
Function name: is_a()
Applicable version: PHP 4, PHP 5, PHP 7
Usage: The is_a() function is used to check whether an object belongs to the specified class or its subclass.
Syntax: bool is_a( object $object, string $class_name )
parameter:
Return value:
Example:
class Person { public $name; } class Student extends Person { public $grade; } $person = new Person(); $student = new Student(); // 检查$person 是否是Person 类的对象if (is_a($person, 'Person')) { echo '$person 是Person 类的对象'; } else { echo '$person 不是Person 类的对象'; } // 检查$student 是否是Person 类的对象if (is_a($student, 'Person')) { echo '$student 是Person 类的对象'; } else { echo '$student 不是Person 类的对象'; } // 检查$student 是否是Student 类的对象if (is_a($student, 'Student')) { echo '$student 是Student 类的对象'; } else { echo '$student 不是Student 类的对象'; }
Output:
$person 是Person 类的对象$student 是Person 类的对象$student 是Student 类的对象
In the above example, we define a Person class and a Student class, which is a subclass of Person class. We create a $person object and a $student object. Use the is_a() function to check the class relationship of these objects. The first check shows that $person is an object of the Person class, the second check shows that $student is also an object of the Person class, and the third check shows that $student is an object of the Student class.